package com.sp.scmdb.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.sp.scmdb.bean.SecurityCommentedBean;
import com.sp.scmdb.common.action.BaseAction;
import com.sp.scmdb.service.SecurityCommentedService;


@Controller("securityCommentedAction")
@Scope("prototype")
public class SecurityCommentedAction extends BaseAction<SecurityCommentedBean>{
	
	private static final long serialVersionUID = 19840810324920L;
	private static Log log = LogFactory.getLog(DaySecurityAction.class);
	private SecurityCommentedService securityCommentedService;
	
	@Autowired
	public void setSecurityCommentedService(
			SecurityCommentedService securityCommentedService) {
		this.securityCommentedService = securityCommentedService;
	}
	
	public String findAll() {
		try {
			DateTimeFormatter formatter =DateTimeFormat.forPattern("yyyy-MM-dd");
			DateTime now = new DateTime();
			
			if(beginDate == null || beginDate.equals("")){
				setBeginDate(now.toString("yyyy-MM-dd"));
			}
			if(endDate == null || endDate.equals("")){
				setEndDate(now.toString("yyyy-MM-dd"));
			}
			
			long time1 = formatter.parseDateTime(beginDate).getMillis();
			long time2 = formatter.parseDateTime(endDate).getMillis();
			long count = (time2 - time1) / (1000 * 3600 * 24);
			List<String> columns = new ArrayList<String>();
			for(int i = 0; i <= count; i++){
				columns.add(formatter.parseDateTime(beginDate).plusDays(i).toString("yyyy-MM-dd"));
			}
			
			setColumnsName(columns);
			
			List<SecurityCommentedBean> securityCommentedList = 
				securityCommentedService.getResultList(beginDate, endDate);
			//entity.setList(securityCommentedList);
			
			entity.setAllList(securityCommentedList);
			entity.setList(securityCommentedList.subList(entity.getCurrentRow(), entity.getLastRow()));
			
		} catch (Exception e) {
			log.error("findAll fail!reason:" + e.getMessage());
			return "fail";
		}
		return SUCCESS;
	}
	
	private List<String> columnsName;

	public List<String> getColumnsName() {
		return columnsName;
	}

	public void setColumnsName(List<String> columnsName) {
		this.columnsName = columnsName;
	}

	private String beginDate;
	private String endDate;

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	
	
	
}
